package defensefirst;

import java.util.Vector;

import battlecode.common.Clock;
import battlecode.common.Direction;
import battlecode.common.GameActionException;
import battlecode.common.GameActionExceptionType;
import battlecode.common.GameConstants;
import battlecode.common.MapLocation;
import battlecode.common.Robot;
import battlecode.common.RobotController;
import battlecode.common.RobotType;
import battlecode.common.Team;


public class RobotPlayer {
	public static void run(RobotController rc) {
		boolean soliderSpawned = false;
		Direction hqLoc = Direction.NONE;
		Direction lastMoved = Direction.NONE;
		Direction moveDirection = Direction.NONE;
		while (true) {
			try {
				
				if (rc.getType() == RobotType.SOLDIER) {
					hqLoc = rc.getLocation().directionTo(rc.senseHQLocation());
					if (rc.isActive() && rc.getTeamPower() > 0) {
						while(true)
						{
							if(rc.senseMine(rc.getLocation()) == Team.NEUTRAL)
							{
								rc.defuseMine(rc.getLocation());
							}
							while(!rc.isActive())
							{
								rc.yield();
							}
							if(rc.senseMine(rc.getLocation()) == rc.getTeam())
							{
								try {
									
									switch(hqLoc)
									{
									case SOUTH:
										moveDirection = Direction.NORTH;
										break;
									case SOUTH_EAST:
										moveDirection = Direction.NORTH_WEST;
										break;
									case EAST:
										moveDirection = Direction.WEST;
										break;
									case NORTH_EAST:
										moveDirection = Direction.SOUTH_WEST;
										break;
									case NORTH:
										moveDirection = Direction.SOUTH;
										break;
									case NORTH_WEST:
										moveDirection = Direction.SOUTH_EAST;
										break;
									case WEST:
										moveDirection = Direction.EAST;
										break;
									case SOUTH_WEST:
										moveDirection = Direction.NORTH_EAST;
										break;
									}
									
									if(rc.senseMine(rc.getLocation().add(moveDirection)) == rc.getTeam().opponent() || rc.senseMine(rc.getLocation().add(moveDirection)) == Team.NEUTRAL)
									{
										
										rc.defuseMine(rc.getLocation().add(moveDirection));
									}
									
									rc.move(moveDirection);
								}catch (GameActionException ex)
								{
									if(ex.getType() == GameActionExceptionType.CANT_MOVE_THERE)
									{
										moveDirection = rc.getLocation().directionTo(rc.senseEnemyHQLocation());
									}
								}
							}
							else
							{
								rc.layMine();
							}
							
							while(!rc.isActive())
							{
								rc.yield();
							}
							
							if(hqLoc.dy == 1 && lastMoved != Direction.SOUTH)
							{
								if(hqLoc.dx != 1)
								{
									lastMoved = Direction.WEST;
									Move(Direction.WEST, rc);
								}
								else
								{
									lastMoved = Direction.SOUTH;
									Move(Direction.SOUTH, rc);
								}
								break;
							}
							
							if(hqLoc.dx == 1 && lastMoved != Direction.EAST)
							{
								if(hqLoc.dy != -1)
								{
									lastMoved = Direction.SOUTH;
									Move(Direction.SOUTH, rc);
								}
								else
								{
									lastMoved = Direction.EAST;
									Move(Direction.EAST, rc);
								}
								break;
							}
							
							if(hqLoc.dy == -1 && lastMoved != Direction.NORTH)
							{
								if(hqLoc.dx != -1)
								{
									lastMoved = Direction.EAST;
									Move(Direction.EAST, rc);
								}
								else
								{
									lastMoved = Direction.NORTH;
									Move(Direction.NORTH, rc);
								}
								break;
							}
							
							if(hqLoc.dx == -1 && lastMoved != Direction.WEST)
							{
								if(hqLoc.dy != 1)
								{
									lastMoved = Direction.NORTH;
									Move(Direction.NORTH, rc);
								}
								else
								{
									lastMoved = Direction.WEST;
									Move(Direction.WEST, rc);
								}
								break;
							}
							
						}
				} 
				}else if (rc.getType() == RobotType.HQ) {
					if (rc.isActive() && !soliderSpawned) {
						// Spawn a soldier
						Direction dir = Direction.NORTH;
						if (rc.canMove(dir)) {
							rc.spawn(dir);
							soliderSpawned = true;
						}
					}
				}

				// End turn
				rc.yield();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private static void Move(Direction moveDirection, RobotController rc) throws GameActionException
	{
		if(rc.canMove(moveDirection))
		{
			MapLocation newLoc = rc.getLocation().add(moveDirection);
			if(rc.senseMine(newLoc) == Team.NEUTRAL || rc.senseMine(newLoc) == rc.getTeam().opponent())
			{
				rc.defuseMine(newLoc);
			}
			while(!rc.isActive())
			{
				rc.yield();
			}
			try
			{
				rc.move(moveDirection);
			}
			catch(GameActionException ex)
			{
				Move(rc.getLocation().directionTo(rc.senseEnemyHQLocation()),rc);
			}
		}
	}
}
